package com.dhcc.core.modules.msgcenter.vo;

import java.io.IOException;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.StringTemplateResourceLoader;

import com.dhcc.core.framework.util.ArrayUtil;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.framework.util.JsonUtil;
import com.dhcc.core.modules.msgcenter.entity.MsgModule;
import com.dhcc.core.modules.msgcenter.entity.MsgPushLog;
import com.dhcc.core.modules.system.entity.Dept;
import com.dhcc.core.modules.system.entity.Group;
import com.dhcc.core.modules.system.entity.Position;
import com.dhcc.core.modules.system.entity.Role;
import com.dhcc.core.modules.system.entity.User;


/**
 * 消息推送
 * 
 * @ClassName: MsgPush
 * @Description: TODO
 * @author cyf
 * @date 2018-11-15 15:02:17
 */
public class MsgPush implements Serializable{
	private static final long serialVersionUID = 1L;

    protected Long logId;
	//推送类型
	private String pushType;	
	//推送时间
	private Date pushTime;	
	//推送尝试次数
	private Integer pushNumber;	
    //接受者类型（0全体 1用户 ）
    private Integer receiveType;    
    //接受者ID
    private String receiveIds;  
    //接受者名称
    private String receiveNames;    
    //归属模块ID
    private Long moduleId;  
    //归属模块名
    private String moduleName;  
    //标题
    private String msgTitle;    
    //消息内容
    private String msgContent;  
    //发送者用户ID
    private Long sendUserId;    
    //发送者用户姓名
    private String sendUserName;   
    //数据
    private String datas;   
    //模块
    private MsgModule module;  
    
    public MsgPush(){
        
    }
    public MsgPush(MsgPushLog log){
        this.logId = log.getId();
        this.pushType = log.getPushType();
        this.pushTime = log.getPushTime();
        this.pushNumber = log.getPushNumber();
        this.receiveType = log.getReceiveType();
        this.receiveIds = log.getReceiveId()+"";
        this.receiveNames = log.getReceiveName();
        this.moduleId = log.getModuleId();
        this.moduleName = log.getModuleName();
        this.msgTitle = log.getMsgTitle();
        this.msgContent = log.getMsgContent();
        this.sendUserId = log.getSendUserId();
        this.sendUserName = log.getSendUserName();
    }
    public Long getLogId() {
        return logId;
    }
    public void setLogId(Long logId) {
        this.logId = logId;
    }
	public void setPushType(String pushType) {
		this.pushType = pushType;
	}
	public String getPushType() {
		return pushType;
	}
	public void setPushTime(Date pushTime) {
		this.pushTime = pushTime;
	}
	public Date getPushTime() {
		return pushTime;
	}
	public void setPushNumber(Integer pushNumber) {
		this.pushNumber = pushNumber;
	}
	public Integer getPushNumber() {
        return pushNumber==null?0:pushNumber;
	}
    public Integer getReceiveType() {
        return receiveType;
    }
    public void setReceiveType(Integer receiveType) {
        this.receiveType = receiveType;
    }
    public String getReceiveIds() {
        return receiveIds;
    }
    public List<Long> getReceiveIdList() {
        if(CommonUtil.isNotEmpty(this.receiveIds)){
            Long[] ar = ArrayUtil.stringToLongArray(this.receiveIds, ArrayUtil.COMMA);
            return Arrays.asList(ar);
        }
        return null;
    }
    public void setReceiveIds(String receiveIds) {
        this.receiveIds = receiveIds;
    }
    public String getReceiveNames() {
        return receiveNames;
    }
    public void setReceiveNames(String receiveNames) {
        this.receiveNames = receiveNames;
    }
    public Long getModuleId() {
        return moduleId;
    }
    public void setModuleId(Long moduleId) {
        this.moduleId = moduleId;
    }
    public String getModuleName() {
        return moduleName;
    }
    public void setModuleName(String moduleName) {
        this.moduleName = moduleName;
    }
    public String getMsgTitle() {
        return msgTitle;
    }
    public void setMsgTitle(String msgTitle) {
        this.msgTitle = msgTitle;
    }
    public String getMsgContent() {
        return msgContent;
    }
    public void setMsgContent(String msgContent) {
        this.msgContent = msgContent;
    }
    public Long getSendUserId() {
        return sendUserId;
    }
    public void setSendUserId(Long sendUserId) {
        this.sendUserId = sendUserId;
    }
    public String getSendUserName() {
        return sendUserName;
    }
    public void setSendUserName(String sendUserName) {
        this.sendUserName = sendUserName;
    }
    public void setSendUser(User sendUser) {
        this.sendUserId = sendUser.getId();
        this.sendUserName = sendUser.getName();
    }
    public void setDatas(String datas) {
        this.datas = datas;
    }
    public String getDatas() {
        return datas;
    }
    public void setDataMap(Map<String,Object> map) {
        if(CommonUtil.isNotEmpty(map)){
            this.datas = JsonUtil.toJson(map);
        }
    }
    public Map<String,Object> getDataMap() {
        if(CommonUtil.isNotEmpty(this.datas)){
            return JsonUtil.jsonToMap(this.datas);
        }
        return null;
    }
    public MsgModule getModule() {
        return module;
    }
    public void setModule(MsgModule module) {
        this.moduleId = module.getId();
        this.moduleName = module.getModuleName();
        this.module = module;
    }
    public void setReceiveUsers(List<User> users) {
        String ids = "";
        String names = "";
        for (int i = 0; i < users.size(); i++) {
            User user = users.get(i);
            ids += i==0?user.getId():(","+user.getId());
            names += i==0?user.getName():(","+user.getName());
        }
        this.receiveIds = ids;
        this.receiveNames = names;
    }
    public void setReceiveDepts(List<Dept> depts) {
        String ids = "";
        String names = "";
        for (int i = 0; i < depts.size(); i++) {
            Dept dept = depts.get(i);
            ids += i==0?dept.getId():(","+dept.getId());
            names += i==0?dept.getName():(","+dept.getName());
        }
        this.receiveIds = ids;
        this.receiveNames = names;
    }
    public void setReceiveGroups(List<Group> groups) {
        String ids = "";
        String names = "";
        for (int i = 0; i < groups.size(); i++) {
            Group group = groups.get(i);
            ids += i==0?group.getId():(","+group.getId());
            names += i==0?group.getName():(","+group.getName());
        }
        this.receiveIds = ids;
        this.receiveNames = names;
    }
    public void setReceiveRoles(List<Role> roles) {
        String ids = "";
        String names = "";
        for (int i = 0; i < roles.size(); i++) {
            Role role = roles.get(i);
            ids += i==0?role.getId():(","+role.getId());
            names += i==0?role.getName():(","+role.getName());
        }
        this.receiveIds = ids;
        this.receiveNames = names;
    }
    public void setReceivePositions(List<Position> positions) {
        String ids = "";
        String names = "";
        for (int i = 0; i < positions.size(); i++) {
            Position position = positions.get(i);
            ids += i==0?position.getId():(","+position.getId());
            names += i==0?position.getName():(","+position.getName());
        }
        this.receiveIds = ids;
        this.receiveNames = names;
    }
    /**
     * 获取推送内容
     * @Title: getMsgContent 
     * @Description: 自动判断是否采用模板
     * @param msg
     * @return
     * @return: String
     */
    public String getMsgContentByTpl() {
        String template = this.getModule().getTemplate();
        Map<String, Object> dataMap = this.getDataMap();
        if(CommonUtil.isNotEmpty(template) && CommonUtil.isNotEmpty(dataMap)){
            StringTemplateResourceLoader resourceLoader = new StringTemplateResourceLoader();
            Configuration cfg;
            try {
                cfg = Configuration.defaultConfiguration();
            } catch (IOException e) {
                return this.getMsgContent();
            }
            GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
            Template t = gt.getTemplate(template);
            t.binding(dataMap);
            return t.render();
        }
        return this.getMsgContent();
    }
}
